package pl.edu.agh.nn.basic;

import java.util.ArrayList;

import pl.edu.agh.nn.conf.InvalidNetworkConfigurationException;
import pl.edu.agh.nn.layer.AbstractLayer;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Layer")
public class BasicLayer extends AbstractLayer<BasicNeuron> {

	@Override
	public void configure() throws InvalidNetworkConfigurationException {
		if (((neurons == null) || neurons.isEmpty())) {
			if (activation == null || neuronCount < 1) {
				throw new InvalidNetworkConfigurationException();
			} else {
				neurons = new ArrayList<BasicNeuron>();
				for (int i = 0; i < neuronCount; i++) {
					BasicNeuron neuron = new BasicNeuron();
					neuron.setActivation(activation);
					neuron.createSynapses(getPreviousLayer().getNeurons());
					neurons.add(neuron);
				}
			}
		} else {
			neuronCount = neurons.size();
			for (BasicNeuron neuron : neurons) {
				if ((neuron.getSynapses() == null) || neuron.getSynapses().isEmpty()) {
					neuron.createSynapses(getPreviousLayer().getNeurons());
				}
				if (neuron.getActivation() == null) {
					neuron.setActivation(activation);
				}
				if (neuron.getActivation() == null) {
					throw new InvalidNetworkConfigurationException();
				}
			}
		}

	}

}
